(function(){
	'use strict';
	angular.module('controllers.workOrderData.problemMgmt')
	.controller('ProblemStatisticController',ProblemStatisticController);
	/**
	 * 衡量指标
	 * 15601：问题单
	 * h4002：已关闭问题
	 * h4003: 已关闭问题所占比率
	 * h4004: 规定时间内解决问题
	 * h4005: 规定时间内解决问题所占比率
	 * h4006: 问题单平均解决时间
	 */
	function ProblemStatisticController($scope,workOrderDataService,CoreLoading,PublicFn,$timeout,problemStatisticFactory,COREDATA){
		$scope.initQuery = function(){
			workOrderDataService.queryMeasureIndex({code:'4'},function(res){
				$scope.measureIndexs = res.data;
				$scope.search();
			});
		};

		$scope.flag = {
			state: true,
			show: 'view',
			arrow: false,
		};
		$scope.sendData = workOrderDataService.getSendData('1');
		$scope.sendData.measureIndexCode = '15601';

		//导出路径
		$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/problemMgmt/export';

		var myEcharts = echarts.init(document.getElementById('doc'));
		$scope.search = function(){
			$scope.flag.state = !$scope.flag.state;
			var xAxisData = [];
			var seriesData = [];
			var option = {};
			var code,obj;
			if(!$scope.flag.state){
				obj = workOrderDataService.fmtSendData($scope.sendData);
				var timeTitle = PublicFn.fmtDateTitle(obj);
				code = obj.measureIndexCode;
				CoreLoading.setState(true);
				problemStatisticFactory.query(obj,function(res){
					if(res.state === '10000' && res.data.length >=1){
						myEcharts.clear();
						$scope.send = obj;
						$scope.measureIndexName = problemStatisticFactory.getName(code);
						if(code === '15601' && $scope.send.changeTypeCode === '1'){
							$scope.send.title = timeTitle + '各来源' + $scope.measureIndexName;
						}else {
							$scope.send.title = timeTitle + '各系统' + $scope.measureIndexName;
						}
						$scope.items = res.data;
						angular.forEach($scope.items,function(item){
							xAxisData.push(item.filedName);
							if(item.filedNum.substring(item.filedNum.length-1) === '%'){
								seriesData.push(Number(item.filedNum.replace('%','')).toFixed(2));
							}else {
								seriesData.push(item.filedNum);
							}
						});
						option = workOrderDataService.getEchartsData(xAxisData,seriesData,$scope.send.title,$scope.measureIndexName);
						option.grid = {
							x2: $scope.measureIndexName.length * 12 + 50
						};
						problemStatisticFactory.fmtOption(code,option);
						myEcharts.setOption(option);
						$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
						$scope.flag.arrow = true;
						$scope.flag.show = 'view';
					}else if(res.data.length === 0){
						workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
					}else {
						workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		};

		$scope.$watch('sendData.measureIndexCode',function(newValue){
			newValue === '15601' ? $scope.sendData.changeTypeCode = '1' : $scope.sendData.changeTypeCode = '';
		});
		//监控页面时间，如果时间出错，则查询按钮禁止
		$scope.$watch('myForm.$invalid', function(newValue) {
			newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
		});

	}
	ProblemStatisticController.$inject = ['$scope','workOrderDataService','CoreLoading','PublicFn','$timeout','problemStatisticFactory','COREDATA'];
})();